﻿@page "/DesignCdmToDoc"
@using JavaDevToys.Rcl.Models
@using JavaDevToys.Rcl.Service
@using System.Reflection
<MAlert Dismissible
        Type="AlertTypes.Error"
        @bind-Value="_alert">
    @errMsg
</MAlert>
<h3>模型转Api文档</h3>
<MContainer Fluid>
    <MRow Align="@AlignTypes.Center">
        <MTextarea Clearable
                   ClearIcon="mdi-close-circle"
                   Label="复制表格内容到此"
                   @bind-Value="cdmText">
        </MTextarea>
    </MRow>
    <MRow Align="@AlignTypes.Center">
        <MButton Color="primary" Class="mt-2  ml-9" OnClick="CreateAndCdmToDoc">生成并复制</MButton>
        <MButton Color="primary" Class="mt-2  ml-9" OnClick="CreateCdmToDoc">生成</MButton>
        <MButton Color="primary" Class="mt-2  ml-9" OnClick="CopyCdmToDoc">复制结果</MButton>
    </MRow>
    <MDivider Class="my-4" />
    <MRow Align="@AlignTypes.Center">
        <MTextarea Clearable
                   ClearIcon="mdi-close-circle"
                   Label="输出内容"
                   @bind-Value="docText">
        </MTextarea>
    </MRow>
</MContainer>
@code {
    private bool _alert = false;
    private string errMsg = string.Empty;
    private string cdmText = string.Empty;
    private string docText = string.Empty;
    [Inject]
    public IClipboardService clipboardService { get; set; }

    private static Type CdmParamType = typeof(CdmParam);
    private static Type ApiDocParamType = typeof(ApiDocParam);
    private PropertyInfo[] CdmParamProperties = CdmParamType.GetProperties();
    private PropertyInfo[] ApiDocParamProperties = ApiDocParamType.GetProperties();

    private List<CdmParam> cdmParams = new List<CdmParam>();
    private List<ApiDocParam> apiDocParams = new List<ApiDocParam>();

    private void CreateAndCdmToDoc()
    {
        CreateCdmToDoc();
        CopyCdmToDoc();
    }

    /// <summary>
    /// 生成doc文档内容
    /// </summary>
    private void CreateCdmToDoc()
    {
        if (string.IsNullOrWhiteSpace(cdmText))
        {
            errMsg = "内容不能为空";
            _alert = true;
            return;
        }
        try
        {
            cdmParams.Clear();
            var lines = cdmText.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            var resultLines = new List<string>();
            foreach (var line in lines)
            {
                CdmParam cdmParam = new CdmParam();
                if (string.IsNullOrWhiteSpace(line))
                    continue;
                var parts = line.Split(new[] { '\t' }, StringSplitOptions.None);
                for (int i = 0; i < parts.Length; i++)
                {
                    CdmParamProperties[i].SetValue(cdmParam, parts[i]?.Trim());
                }
                cdmParams.Add(cdmParam);
            }
            cdmText = string.Join(Environment.NewLine, resultLines);
        }
        catch (Exception ex)
        {
            errMsg = $"生成失败: {ex.Message}";
            _alert = true;
        }
    }

    private void CopyCdmToDoc()
    {
        clipboardService.SetTextAsync(docText);
    }
}
